package com.processmining.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.processmining.entity.Algo;
import com.processmining.entity.EventLog;
import com.processmining.entity.MergeEventLog;
import com.processmining.entity.User;
import com.processmining.service.IAlgoService;
import com.processmining.service.IEventLogService;
import com.processmining.service.IMergeEventLogService;
import com.processmining.util.algo.MergeUtil;
import com.processmining.util.request.IdList;
import com.processmining.util.request.MergeRequest;
import com.processmining.util.response.Code;
import com.processmining.util.response.ResponseData;
import com.processmining.util.response.ResponseWithListPage;
import com.processmining.util.response.ResponseWithPayload;

@Controller
public class MergeController {
	   
	   @Autowired
	   private IEventLogService eventLogService;
	   
	   @Autowired
	   private IAlgoService algoService;
	   
	   @Autowired
	   private MergeUtil mergeUtil;
	   
	   @Autowired
	   private IMergeEventLogService melService;
	   
	   @ResponseBody
	   @RequestMapping(value="merge")
       public Object merge(@RequestBody MergeRequest mr,HttpSession session) throws Exception{
    	    EventLog evtLog1=(EventLog) eventLogService.selectByPK(mr.getEvtLog1Id());
    	    EventLog evtLog2=(EventLog) eventLogService.selectByPK(mr.getEvtLog2Id());
    	    Algo algo=algoService.getAlgoById(mr.getAlgoId());
    	    User u=(User) session.getAttribute("user");
    	    Integer uid = u!=null?u.getId():1;
    	    Long currentTime=System.currentTimeMillis();
    	    EventLog result=mergeUtil.merge(evtLog1, evtLog2, algo, mr.getParam(),uid);
    	    Long endTime=System.currentTimeMillis();
    	    Long duration=endTime-currentTime;
    	    
    	    HashMap<String,Object> res=new HashMap<String,Object>();
    	    res.put("time",duration);
    	    res.put("resultLog",result);

    	    ResponseWithPayload response = new ResponseWithPayload(Code.SUCCEED, res);
    	    return response;
       }
	   
	   @ResponseBody
	   @RequestMapping(value="listAllMEL")
	   public ResponseWithListPage<MergeEventLog> listAllMergeEventLog(
				@RequestParam(value="pageNum",defaultValue="1")int pageNum,
				@RequestParam(value="pageSize",defaultValue="10")int pageSize,
				@RequestParam(value="info",required=false) String info,
				HttpSession session){
		   User user = (User) session.getAttribute("user");
		   //Integer uid = user.getId();
		   ResponseWithListPage<MergeEventLog> ret = new ResponseWithListPage<>();
		   PageInfo<MergeEventLog> logListWithPage = melService.listAllMergeEventLog(pageNum, pageSize,1);
		   ret.setCode(Code.SUCCEED);
		   ret.setData(logListWithPage);
		   return ret;
	   }
	   
	   
	   @ResponseBody
	   @RequestMapping("/mergeLog/listMergeAlgo")
	   public List<Algo> listMergeAlgo(){
		   return algoService.listMergeAlgo();
	   }
	   	   
	   @ResponseBody
	   @RequestMapping(value="search")
	   public ResponseWithListPage<MergeEventLog> search(@RequestParam(value="pageNum",defaultValue="1")int pageNum,
				@RequestParam(value="pageSize",defaultValue="10")int pageSize,
				@RequestParam(value="searchInfo") String searchInfo,
				HttpSession session){
		   User user = (User) session.getAttribute("user");
		   ResponseWithListPage<MergeEventLog> ret = new ResponseWithListPage<>();
		   PageInfo<MergeEventLog> logListWithPage = melService.search(pageNum, pageSize,user.getId(),searchInfo);
		   ret.setCode(Code.SUCCEED);
		   ret.setData(logListWithPage);
		   return ret;
		   
	   }

	   @ResponseBody
	   @RequestMapping(value="/mergeLog/deleteBatch")
	   public ResponseData deleteBatch(@RequestBody IdList idList,HttpSession session) throws Exception{
		   User user = (User) session.getAttribute("user");
		   melService.deleteBatch(idList.getIdList());
		   ResponseData reponse = new ResponseData();
		   reponse.setCode(Code.SUCCEED);
		   
		   return reponse;
		   
	   }
}
